package com.jxs.android.ges;

import java.io.IOException;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.HandlerHolder;
import com.jxs.android.weiget.WorkLogEditItem;

public class WorkLogSaveHolder {

	private Handler handler;
	private String wl_id = "";// 工作日志ID

	// 2.4.5 saveWorkLog 【保存工作日志接口】
	public WorkLogSaveHolder(Handler handler, String wl_id) {
		this.handler = handler;
		this.wl_id = wl_id;
	}

	private String getValue(List<WorkLogEditItem> items, boolean isMark) {

		StringBuilder rs = new StringBuilder("");

		for (int id = 0; id < items.size(); id++) {
			WorkLogEditItem item = items.get(id);
			if (isMark) {
				rs.append(item.getNote());
			} else {
				rs.append(item.getContent());
			}
			if (id != (items.size() - 1))
				rs.append("|");
		}

		return rs.toString();
	}

	public void saveWorkLog(final String ticket, final String create_date, final List<WorkLogEditItem> todayListWorkLogEditItems,
			final List<WorkLogEditItem> tomorrowListWorkLogEditItems) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "saveWorkLog";//
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("wl_id", wl_id);
				request.addProperty("create_date", create_date);// 日期
				request.addProperty("today_content", getValue(todayListWorkLogEditItems, false));// 今日工作-工作内容
				request.addProperty("today_reamrk", getValue(todayListWorkLogEditItems, true));// 今日工作-备注
				request.addProperty("tomorrow_content", getValue(tomorrowListWorkLogEditItems, false));// 明日计划-工作内容
				request.addProperty("tomorrow_reamrk", getValue(tomorrowListWorkLogEditItems, true));// 明日计划-备注

				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser_new_Interface(response);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = HandlerHolder.What.ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}

				});
			}
		}).start();
	}

	/**
	 * @Des this method will be used to when update interface protocol
	 * @param soapObject
	 * @param params
	 */
	private void paser_new_Interface(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				Message msg = new Message();
				SoapObject detail = (SoapObject) soapObject.getProperty("detail");
				wl_id = SoapUtil.getValue(detail, "wl_id");
				msg.what = HandlerHolder.What.WORK_LOG_SAVE_RESLT_OK;
				Bundle bundle = new Bundle();
				bundle.putString("wl_id", wl_id);
				msg.setData(bundle);
				msg.obj = message;
				handler.sendMessage(msg);
			}
		}
	}

}
